package com.itheima.bos.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {

	private static String smtp_host ="smtp.163.com";//网易服务网址
	private static String username="yuhaonanwork@163.com";  //用户名
	private static String password="q5313811455"; //邮箱授权码
	
	private static String from="yuhaonanwork@163.com";//可以使用当前账户
	public static String activeUrl="http//:localhost:9003/bos_fore/customer_activeMail";
	
	//发送邮件的方法  subject 代表的是 发送文件的名称    context 代表要发送的内容    to 代表的是要发送的邮箱帐号
	public static void sendMail(String subject ,String content,String to){
		//创建一个Properties对象用于存储参数
		Properties properties = new Properties();
		properties.setProperty("mail.smtp.host", smtp_host);//虚拟主机
		properties.setProperty("mail.transport.protocol", "stmp");//mail的传输协议
		properties.setProperty("mail.smtp.auth", "true");
		//创建一个session
		Session session = Session.getInstance(properties);
		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			message.setSubject(subject);
			message.setContent(content, "text/html;charset=utf-8");
			Transport transport = session.getTransport();
			transport.connect(smtp_host, username, password);
			transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("邮件发送失败...");
		}
		
		
	}
}
